﻿using System;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class AddNewContractor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
        }

        protected void ButtonAddNewClick(object sender, EventArgs e)
        {
            if (checkAutoAccount.Checked)
            {
                var userName = AdministrationUtilityLogic.AutoUserName(textFullName.Text);
                // Add new account for contractor
                var accountEntity = new AccountEntity
                                        {
                                            UserName = userName,
                                            Password = "123456",
                                            // Default password
                                            RoleId = (int) RoleList.Contractor,
                                            Status = 1,
                                            ActorType = "Contractor"
                                        };
                var result = AdministrationUtilityLogic.AddNewAccount(accountEntity);

                var eContractor = new ContractorEntity
                                      {
                                          UserName = userName,
                                          FullName = Server.HtmlEncode(textFullName.Text),
                                          Email = Server.HtmlEncode(textEmail.Text),
                                          PhoneNumber1 = Server.HtmlEncode(textPhoneNumber1.Text),
                                          PhoneNumber2 = Server.HtmlEncode(textPhoneNumber2.Text),
                                          Address = Server.HtmlEncode(textAddress.Text),
                                          Branch = Server.HtmlEncode(textBranch.Text),
                                          SortCode = Server.HtmlEncode(textSortCode.Text)
                                      };
                var insert = ContractorProfileLogic.InsertContractor(eContractor);

                if (insert == "Successfully!" && result == "Successfully!")
                {
                    Response.Redirect("ListContractor.aspx");
                }
                ResultMessage.Text = insert;
                UserEventEntity(insert);
            }
            else
            {
                var eContractor = new ContractorEntity
                {
                    UserName = "",
                    FullName = textFullName.Text,
                    Email = textEmail.Text,
                    PhoneNumber1 = textPhoneNumber1.Text,
                    PhoneNumber2 = textPhoneNumber2.Text,
                    Address = textAddress.Text,
                    Branch = textBranch.Text,
                    SortCode = textSortCode.Text
                };
                var insert = ContractorProfileLogic.InsertContractor(eContractor);

                if (insert == "Successfully!")
                {
                    Response.Redirect("ListContractor.aspx");
                }
                ResultMessage.Text = insert;
                UserEventEntity(insert);
            }
        }

        protected void ButtonResetClick(object sender, EventArgs e)
        {
            textFullName.Text = string.Empty;
            textEmail.Text = string.Empty;
            textPhoneNumber2.Text =string.Empty;
            textPhoneNumber1.Text = string.Empty;
            textAddress.Text = string.Empty;
            textBranch.Text = string.Empty;
            textSortCode.Text = string.Empty;
        }
        //event of insert client
        private void UserEventEntity(string userEvent)
        {
            // Save event of insert client
            var userEventEntity = new EventEntity
            {
                UserName = CommonLogic.ConvertToString(Session["UserName"]),
                EventType = (int)EventTypeList.UserEvent,
                OnDate = DateTime.Now,
                Description = "User Insert client " + userEvent,
            };
            EventTracer.SaveEvents(userEventEntity);
            ResultMessage.Visible = true;
            ResultMessage.Text = userEvent;
        }
        //check user permission
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.AddConsultant;

            if (Page.Master == null) return;
            var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

            AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
        }
    }
}
